package ${packageName}.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.layer.cms.common.result.ResponseData;
import ${packageName}.dto.${className}Dto;
import ${packageName}.mapper.${className}Mapper;
import ${packageName}.pojo.${className};
import ${packageName}.service.${className}Service;
import ${packageName}.vo.${className}Vo;

/**
* ${remark}
* @author: ${operationSupport}
* @date: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
*/
@Service
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {

	
  	/**
	 * @description: 分页条件查询
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
     * @param dto
	 * @return ResponseData
	 */
	@Override
	public ResponseData<List<${className}Vo>> list(${className}Dto dto) {

		IPage<${className}> page = new Page<${className}>(dto.getPage(), dto.getLimit());
		
		LambdaQueryWrapper<${className}> queryWrapper = new LambdaQueryWrapper<${className}>();
		//queryWrapper.orderByDesc(${className}::getSort);
		
		// TODO: 编写查询条件
		
		
		IPage<${className}> ipage = this.page(page, queryWrapper);

		List<${className}Vo> list = new ArrayList<${className}Vo>();
		for (${className} bean : ipage.getRecords()) {
			${className}Vo vo = new ${className}Vo();
			BeanUtils.copyProperties(bean, vo);
			list.add(vo);
		}
		return ResponseData.ok(200, "success", list, ipage.getTotal());
	}
	
	/**
	 * @description: 根据ID查询详情
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 * @param id ID
	 */
	@Override
	public ResponseData<${className}Vo> queryById(String id) {

		${className} bean = this.getById(id);
		${className}Vo vo = new ${className}Vo();
		BeanUtils.copyProperties(bean, vo);
		
		return ResponseData.ok(200, "success", vo);
	}

  	/**
	 * @description: 添加或者修改; ID为空添加
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 * @param  vo
	 * @return true: 数据修改成功; false: 数据修改失败
	 */
	@Override
	public ResponseData<String> insertOrUpdate(${className}Dto vo) {
	
		${className} bean = new ${className}();
		BeanUtils.copyProperties(vo, bean);
		
		boolean bool = this.saveOrUpdate(bean);
		if(bool){
			return ResponseData.ok(200, "数据保存成功...");
		}
		return ResponseData.fail(500, "数据保存失败...");
	}

  	/**
	 * @description: 根据ID删除数据-物理删除
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 * @param id 操作ID
	 * @return 删除数据的条数
	 */
	@Override
	public ResponseData<String> delete(String id) {
		int num = baseMapper.deleteById(id);
		if(num == 1){
			return ResponseData.ok(200, "数据删除成功...");
		}
		return ResponseData.fail(500, "数据删除失败...");
	}
	
	
  	/**
	 * @description: 禁用或者启用
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 * @param id		操作ID
	 * @param status	状态; 0: 禁用; 1: 启用
	 * @return 数据修改条数
	 */
	@Override
	public ResponseData<String> state(String id, boolean status) {
		int num = baseMapper.updateState(id, status ? 1 : 0);
		if(num == 1){
			return ResponseData.ok(200, "数据状态修改成功...");
		}
		return ResponseData.fail(500, "数据状态修改失败...");
	}
	
	/**
	 * @description: 批量删除数据
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 * @param list
	 * @return 删除条数
	 */
	@Override
	public ResponseData<String> batchDelete(List<> list) {

		List<String> idList = list.stream().filter(e -> !ObjectUtils.isEmpty(e.getId())).map(e -> e.getId()).collect(Collectors.toList());
		this.removeByIds(idList);
		int num = list.size();
		if(num > 0){
			return ResponseData.ok(200, "成功删除了" + num + "条数据");
		}
		return ResponseData.fail(500, "数据删除失败...");
	}
	
	/**
	 * @description: 批量启用
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 * @param list
	 * @return 启用条数
	 */
	@Override
	public ResponseData<String> online(List<> list) {
	
		for (${className}Dto bean : list) {
			baseMapper.updateState(bean.getId(), 1);
		}
		int num = list.size();
		if(num > 0){
			return ResponseData.ok(200, "状态修改成功...");
		}
		return ResponseData.fail(500, "状态修改失败...");
	}

	/**
	 * @description: 批量禁用
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 * @param list
	 * @return 禁用条数
	 */
	@Override
	public ResponseData<String> offline(List<${className}Dto> list) {
		for (${className}Dto bean : list) {
			baseMapper.updateState(bean.getId(), 0);
		}
		int num = list.size();
		if(num > 0){
			return ResponseData.ok(200, "状态修改成功...");
		}
		return ResponseData.fail(500, "状态修改失败...");
	}
	
	/**
	 * @description: 清空表所有数据
	 * @author: ${operationSupport}
	 * @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	 */
	@Override
	public ResponseData<String> deleteAll() {
		baseMapper.cleanTable();
		return ResponseData.ok(200, "表数据已清空");
	}

	/**
	* @description: 获取所有ids
	* @author: ${operationSupport}
	* @data: ${.now?string["yyyy-MM-dd HH:mm:ss"]}
	*/
	@Override
	public ResponseData<List<String>> queryIds() {
		List<String> list = baseMapper.queryIds();
		return ResponseData.ok(200, "success", list);
	}
}
